package com.example.demo.service;

import com.example.demo.dao.BookInfoMapper;
import com.example.demo.dao.OrderDetailMapper;
import com.example.demo.entity.BookInfo;
import com.example.demo.entity.OrderDetail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Override
    public List<BookInfo> findBookInfoByOrderId(String orderId) {

        Example example=new Example(OrderDetail.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        List<OrderDetail> bookDetails=orderDetailMapper.selectByExample(example);

        List<BookInfo> bookInfos=new ArrayList<>();

        for(OrderDetail orderDetail:bookDetails){
            BookInfo  bookInfo=bookInfoMapper.selectByPrimaryKey(orderDetail.getBookId());
            bookInfos.add(bookInfo);
        }

        return bookInfos;
    }
}
